<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\logic\room;


use app\common\model\room\RoomApartmentDevice;
use app\common\logic\BaseLogic;
use think\facade\Db;


/**
 * 公寓设备关联逻辑
 * Class RoomApartmentDeviceLogic
 * @package app\adminapi\logic\room
 */
class RoomApartmentDeviceLogic extends BaseLogic {


    /**
     * @notes 添加公寓设备关联
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public static function add(array $params): bool {
        Db::startTrans();
        try {
            $params['use_total'] = $params['reading'] - $params['base_number'];
            $params['total_fee'] = ($params['reading'] - $params['base_number']) * $params['coefficient'] * $params['price'];

            $save_data = [
                'cid' => $params['cid'],
                'bid' => $params['bid'],
                'rid' => $params['rid'],
                'status' => $params['status'],
                'device_id' => $params['device_id'],
                'coefficient' => $params['coefficient'],
                'price' => $params['price'],
                'base_number' => $params['base_number'],
                'reading' => $params['reading'],
                'use_total' => $params['use_total'],
                'lift_fee' => $params['lift_fee'],
                'second_pressure_fee' => $params['second_pressure_fee'],
                'total_fee' => sprintf("%.2f", $params['total_fee']),
//                'log_time' => $params['log_time'],
                'add_time' => date("Y-m-d H:i:s"),
                'update_time' => date("Y-m-d H:i:s"),
                'device_name' => $params['device_name'],
                'device_no' => $params['device_no'],
                'device_type' => $params['device_type'],
            ];
            if ($params['reading'] && $params['reading'] * 1 > 0) {
                $save_data['log_time'] = date("Y-m-d H:i:s");
            }
            RoomApartmentDevice::create($save_data);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑公寓设备关联
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public static function edit(array $params): bool {
        Db::startTrans();
        try {
            $params['total_fee'] = ($params['reading'] - $params['base_number']) * $params['coefficient'] * $params['price'];

            if (!empty($params['reading']) && !empty($params['base_number'])) {
                $params['use_total'] = $params['reading'] - $params['base_number'];
            }
            if (!empty($params['coefficient']) && !empty($params['price']) && !empty($params['base_number']) && !empty($params['reading'])) {
                $params['total_fee'] = ($params['reading'] - $params['base_number']) * $params['coefficient'] * $params['price'];
            }

            $save_data = [
                'cid' => $params['cid'],
                'bid' => $params['bid'],
                'rid' => $params['rid'],
                'status' => $params['status'],
                'device_id' => $params['device_id'],
                'coefficient' => $params['coefficient'],
                'price' => $params['price'],
                'base_number' => $params['base_number'],
                'reading' => $params['reading'],
                'use_total' => $params['use_total'],
                'lift_fee' => $params['lift_fee'],
                'second_pressure_fee' => $params['second_pressure_fee'],
                'total_fee' => sprintf("%.2f", $params['total_fee']),
                'update_time' => date("Y-m-d H:i:s"),
                'device_name' => $params['device_name'],
                'device_no' => $params['device_no'],
                'device_type' => $params['device_type'],
            ];
            if ($params['reading'] && $params['reading'] * 1 > 0) {
                $save_data['log_time'] = date("Y-m-d H:i:s");
            }
            RoomApartmentDevice::where('id', $params['id'])->update($save_data);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function setSave(array $params): bool {
        Db::startTrans();
        $params['total_fee'] = ($params['reading'] - $params['base_number']) * $params['coefficient'] * $params['price'];

        if (!empty($params['reading']) && !empty($params['base_number'])) {
            $params['use_total'] = $params['reading'] - $params['base_number'];
        }
        if (!empty($params['coefficient']) && !empty($params['price']) && !empty($params['base_number']) && !empty($params['reading'])) {
            $params['total_fee'] = ($params['reading'] - $params['base_number']) * $params['coefficient'] * $params['price'];
        }

        try {
            $save_data = [
                'update_time' => date("Y-m-d H:i:s"),
                'coefficient' => $params['coefficient'],
                'price' => $params['price'],
                'base_number' => $params['base_number'],
                'reading' => $params['reading'],
                'use_total' => $params['use_total'],
                'total_fee' => sprintf("%.2f", $params['total_fee']),
//                'lift_fee' => $params['lift_fee'],
//                'second_pressure_fee' => $params['second_pressure_fee'],
            ];
            if ($params['reading'] && $params['reading'] * 1 > 0) {
                $save_data['log_time'] = date("Y-m-d H:i:s");
            }
            RoomApartmentDevice::where('id', $params['id'])->update($save_data);
            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除公寓设备关联
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public static function delete(array $params): bool {
        $id = $params['id'];
        if (!is_array($id)) {
            $id = [$id];
        }
        $ids = RoomApartmentDevice::where([
            ["room_master", "=", "0"],
            ["id", "in", $id],
        ])->column("id");
        return RoomApartmentDevice::destroy($ids);
    }


    /**
     * @notes 获取公寓设备关联详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/02/26 16:32
     */
    public static function detail($params): array {
        return RoomApartmentDevice::findOrEmpty($params['id'])->toArray();
    }
}
